<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script type="text/javascript">
    // 1.创建对象
    window.onload = function() {
        var user = document.getElementById('user');
        user.onblur = function() {
            var user = document.getElementById('user');
            var username = user.value;
            var pwd = document.getElementById('passw').value;
            // console.log(username);
            var xhr = null;
            if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest()
            } else {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }

            // 2准备发送
            var parm = 'user=' + username + "&password=" + pwd;
            // 解决ie浏览器乱码的问题  encodeURI()用来对中文参数进行编码，防止乱码
            xhr.open('post', './post.php');
            // 进行post
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            // 3执行发送
            xhr.send(parm);
            // 4.执行回调函数
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        // alert("hello");
                        // alert(xhr.responseText)
                        var span = document.getElementById('ineruser');
                        span.innerHTML = xhr.responseText
                    }
                }

            }
        }
    }
</script>

<body>
    <form action="">
        <div>用户名：
            <input type="text" name="username" id="user">
            <span id="ineruser"></span>
        </div>
        <div>密码：
            <input type="text" name="password" id="passw">
        </div>
        <input type="submit" value="注册" id="btn">
    </form>
</body>

</html>